package com.xk.sport.backend.web.controller;

import com.xk.sport.backend.common.annotation.Auth;
import com.xk.sport.backend.common.annotation.Login;
import com.xk.sport.backend.common.constant.ResultStatus;
import com.xk.sport.backend.common.entity.Result;
import com.xk.sport.backend.dao.model.Area;
import com.xk.sport.backend.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Login
@Controller
@RequestMapping(value = "/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 查询
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        List<Area> list = areaService.findAll();
        return new Result(ResultStatus.success.getStatus(), "", list);
    }

    /**
     * 查询
     */
    @RequestMapping("/findOne")
    @ResponseBody
    public Result findOne(long id) {
        Area area = areaService.findOne(id);
        return new Result(ResultStatus.success.getStatus(), "", area);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Area area) {
        Area newArea = areaService.add(area);
        if (newArea == null) {
            return new Result(ResultStatus.normal_error.getStatus(), "添加失败");
        } else {
            return new Result(ResultStatus.success.getStatus(), "", newArea);
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Area area) {
        Area newArea = areaService.update(area);
        if (newArea == null) {
            return new Result(ResultStatus.normal_error.getStatus(), "修改失败");
        } else {
            return new Result(ResultStatus.success.getStatus(), "", newArea);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(long id) {
        if (!areaService.delete(id)) {
            return new Result(ResultStatus.normal_error.getStatus(), "删除失败");
        } else {
            return new Result(ResultStatus.success.getStatus(), "");
        }
    }
}
